﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;

namespace DAO
{
    public class NhanVienDAO
    {
        public DataTable LoadDSNV()
        {
            Data a = new Data();
            string sql = "SELECT * FROM NhanVien where IsDelete = 0";
            DataTable dt = new DataTable();
            dt = a.ExecuteQuery(sql);

            return dt;
        }

        public Boolean KiemTraTrung(string CMND)
        {
            Data a = new Data();
            string sql = "select * from NhanVien where CMND ='" + CMND + "' ";
            if (a.ExecuteQuery(sql).Rows.Count > 0)
                return true;

            return false;
        }

        public Boolean CapNhatTrungNV(NhanVienDTO nvDTO)
        {
            Data a = new Data();
            string sql = "Update NhanVien set IsDelete = 0, TenNV = N'" + nvDTO.HoTen + "', NgaySinh = '" + nvDTO.NgaySinh + "', GioiTinh = '" + nvDTO.GioiTinh + "', DiaChi ='" + nvDTO.DiaChi + "', SDT ='" + nvDTO.SDT + "' where CMND = '" + nvDTO.CMND + "'";
            return a.ExecuteNonQuery(sql);
        }

        public Boolean ThemNV(NhanVienDTO nvDTO)
        {
            Data a = new Data();
            string sql = "insert into NhanVien(TenNV, CMND, NgaySinh, GioiTinh, DiaChi,SDT,IsDelete) ";
            sql += "values( N'" + nvDTO.HoTen + "',";
            sql += " '" + nvDTO.CMND + "',";
            sql += " '" + nvDTO.NgaySinh + "',";
            sql += " N'" + nvDTO.GioiTinh + "',";
            sql += " N'" + nvDTO.DiaChi + "',";
            sql += " '" + nvDTO.SDT + "', 0)";

            return a.ExecuteNonQuery(sql);
        }

        public Boolean XoaNV(int maNV)
        {
            Data a = new Data();
            string sql = "Update NhanVien set IsDelete = 1 where MaNV = '" + maNV + "'";

            return a.ExecuteNonQuery(sql);
        }

        public Boolean CapNhat(NhanVienDTO nvDTO)
        {
            Data a = new Data();
            string sql = "Update NhanVien set TenNV = N'" + nvDTO.HoTen + "', ";
            sql += " CMND = '" + nvDTO.CMND + "',";
            sql += " NgaySinh = '" + nvDTO.NgaySinh + "',";
            sql += " GioiTinh = N'" + nvDTO.GioiTinh + "',";
            sql += " DiaChi = N'" + nvDTO.DiaChi + "',";
            sql += " SDT = '" + nvDTO.SDT + "' where MaNV = " + nvDTO.MaNV + " ";

            return a.ExecuteNonQuery(sql);
        }
    }
}
